package com.hook3.pianke.Adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hook3.pianke.R;
import com.hook3.pianke.domain.CommentInfo;
import com.hook3.pianke.domain.TopicInfoDetail;
import com.hook3.pianke.view.CircleImageView;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.List;

/**
 * 商品列表
 * Created by HOME on 2015/12/8.
 */
public class TalkAdapter extends BaseAdapter {
    //数据
    private Activity activity;
    private ViewHolder viewHolder;
    private final List<CommentInfo> commentlist;

    public TalkAdapter(Activity activity, TopicInfoDetail infoDetail) {
        this.activity = activity;
        commentlist = infoDetail.getCommentlist();
    }


    @Override
    public int getCount() {
        return commentlist.size();
    }

    @Override
    public Object getItem(int position) {
        return commentlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(activity, R.layout.item_talk_comment, null);
            viewHolder.adapter_talk_comment_head_img = (CircleImageView) convertView.findViewById(R.id.adapter_talk_comment_head_img);
            viewHolder.adapter_talk_comment_user_name_tx = (TextView) convertView.findViewById(R.id.adapter_talk_comment_user_name_tx);
            viewHolder.adapter_talk_comment_flow_size_tx = (TextView) convertView.findViewById(R.id.adapter_talk_comment_flow_size_tx);
            viewHolder.adapter_talk_comment_desc_tx = (TextView) convertView.findViewById(R.id.adapter_talk_comment_desc_tx);
            viewHolder.adapter_talk_comment_cover_img = (ImageView) convertView.findViewById(R.id.adapter_talk_comment_cover_img);
            viewHolder.adapter_talk_comment_pic_view = (RelativeLayout) convertView.findViewById(R.id.adapter_talk_comment_pic_view);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CommentInfo commentInfo = commentlist.get(position);
        ImageView imageView = new ImageView(activity);
        x.image().bind(imageView, commentInfo.getUserinfo().getIcon(), new MyCommonCallback(viewHolder.adapter_talk_comment_head_img));
        viewHolder.adapter_talk_comment_user_name_tx.setText(commentInfo.getUserinfo().getUname());
        viewHolder.adapter_talk_comment_flow_size_tx.setText(commentInfo.getCmtnum() + "楼 " + commentInfo.getAddtime_f());
        viewHolder.adapter_talk_comment_desc_tx.setText(commentInfo.getContent());
        if (!commentInfo.getCoverimg().isEmpty()) {
            x.image().bind(viewHolder.adapter_talk_comment_cover_img, commentInfo.getCoverimg());
        } else {
            viewHolder.adapter_talk_comment_pic_view.setVisibility(View.GONE);
        }

        return convertView;
    }

    class MyCommonCallback implements Callback.CommonCallback<Drawable> {
        CircleImageView adapter_talk_comment_head_img;

        public MyCommonCallback(CircleImageView adapter_talk_comment_head_img) {
            this.adapter_talk_comment_head_img = adapter_talk_comment_head_img;
        }

        @Override
        public void onSuccess(Drawable result) {
            adapter_talk_comment_head_img.setImageDrawable(result);
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    }

    class ViewHolder {
        public CircleImageView adapter_talk_comment_head_img;
        public TextView adapter_talk_comment_user_name_tx;
        public ImageView adapter_talk_comment_master_img;
        public ImageView adapter_talk_comment_good_img;
        public TextView adapter_talk_comment_good_count_tx;
        public TextView adapter_talk_comment_flow_size_tx;
        public TextView adapter_talk_comment_time_tx;
        public TextView adapter_talk_comment_desc_tx;
        public ImageView adapter_talk_comment_cover_img;
        public TextView adapter_talk_comment_pic_count_tx;
        public RelativeLayout adapter_talk_comment_pic_view;
    }

    class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            String url = (String) v.getTag();
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(url);
            intent.setData(content_url);
            activity.startActivity(intent);
        }
    }
}
